import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbfdrolemenu } from '../../../../../atoms/db';

const logger = anylogger('pages/api/admin/role/menus/getuserole.api');

export type Result = string[];

export type Message = {
	roleid: string;
}

/**
 * 获取当前角色分配的菜单id
 */
const handler = an48<Result>();

handler.get(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { roleid } = req.query as Message;
		if (!roleid) {
			res.status(200).json([]);
			return;
		}
		const d = await tbfdrolemenu()
			.select('menuid')
			.where({
				roleid
			});
		const data = d.map((it) => {
			return it.menuid;
		});
		res.status(200).json(data);
	} catch (error) {
		logger.trace(error);
		res.status(200).json([]);
	}
});

export const config = {} as PageConfig;

export default handler;
